package xyz.scootaloo.oj.iq

import org.junit.Test

/**
 * @author flutterdash@qq.com
 * @since 2021/4/16 15:17
 */
class M0108 {

    private fun setZeroes(matrix: Array<IntArray>) {
        val rowSet = HashSet<Int>()
        val columnSet = HashSet<Int>()
        for (r in matrix.indices) {
            for (c in matrix[0].indices) {
                if (matrix[r][c] == 0) {
                    rowSet.add(r)
                    columnSet.add(c)
                }
            }
        }

        // 清除行
        for (row in rowSet) {
            for (c in matrix[0].indices)
                matrix[row][c] = 0
        }

        // 清除列
        for (column in columnSet) {
            for (r in matrix.indices)
                matrix[r][column] = 0
        }
    }

    @Test
    fun test0() {
        val input = arrayOf(
            intArrayOf(1, 1, 1),
            intArrayOf(1, 0, 1),
            intArrayOf(1, 1, 1),
        )
        setZeroes(input)
        for (array in input)
            println(array.contentToString())
    }

    @Test
    fun test1() {
        val input = arrayOf(
            intArrayOf(0, 1, 2, 0),
            intArrayOf(3, 4, 5, 2),
            intArrayOf(1, 3, 1, 5),
        )
        setZeroes(input)
        for (array in input)
            println(array.contentToString())
    }

}
